// pages/02admin/03table/table.js
var app = getApp()
var appData = app.globalData
var co = require('../../js/common.js')
var i, j
var _this

Page({
   data: {
      isShowInputLabelArr: [0, 0, 0, 0],
      tableList: '',
      cate: null,
      edit_id: -1,
      pick_i: -1,
      //printerList: [],
      management: [],
      mode: '',
      buttonText: "新增餐桌类型",
      exchange_id: -1,
      edit: '',
   },
   onLoad: function (options) {
      _this = this
      this.getAllTableList()
   },
   sorting: function () {
      this.setData({ mode: this.data.mode == '' ? 'sort' : '' })
      console.log('this.data====', this.data)
   },
   //餐桌排序
   toExchange2: function (e) {
      var arr = this.data.tableList      //获取分区及餐桌信息
      var index = e.currentTarget.id
      if (index == '') {
         this.setData({ exchange_id: -1 })
         return
      }
      var iArr = index.split('-')
      var td = this.data
      var t = this
      if (td.exchange_id == -1) {
         t.setData({ exchange_id: index == '' ? -1 : index })
         i = iArr[0]
         j = iArr[1]
      } else {
         var param = {
            cateId1: arr[iArr[0]].cateId,
            cateId2: arr[i].cateId,
            Table_ID1: arr[iArr[0]].TableList[iArr[1]].Table_ID,
            Table_ID2: arr[i].TableList[j].Table_ID,
         }
         // console.log("【获取param】===", param)
         app.getJsonData('table/Tmg/exchangeTable', param, function (res) {
            // console.log("【返回信息】===",res.data)
            t.setData({
               tableList: res.data,
               exchange_id: -1
            })
         })
         _this.getAllTableList()
      }
   },
   //餐桌管理
   arrange: function (e) {
      var td = this.data
      var t = this
      var param = {
         shop_id: appData.shopInfo.Biz_ID
      }
      //tp5
      app.getNetData('table/manage/tableSortOut', param, function (res) {
         t.setData({
            tableCateList: res.data
         })
         appData.tableCateArr = res.data
      })
   },

   editTable2: function () {
      this.setData({
         mode: this.data.mode == '' ? 'add' : '',
         buttonText: this.data.mode == '' ? '取消新增' : '新增餐桌',
         edit_id: -1,
         isShowInputLabelArr: [0, 0, 0, 0]
      })
   },

   pickerClick: function (e) {
      this.setData({//页面用分类数据
         pick_i: e.detail.value,
         isShowInputLabelArr: [1, 0, 0, 0],
      })
   },
   //编辑按钮 
   editable: function (e) {
      var index = e.currentTarget.id
      var idx = e.currentTarget.dataset.index      //获取点击的位置
      var td = this.data
      this.setData({
         edit: idx,
         edit_id: td.edit_id == index ? -1 : index,
         isShowInputLabelArr: td.edit_id == index ? [1, 1, 1, 1] : [0, 0, 0, 0],
         mode: '',
      })
      // console.log("【获取点击id】===", td.edit_id == index)
   },
   //添加或修改
   submitTableInfo: function (e) {
      var t = this
      var td = t.data
      var apiURL
      var param = e.detail.value
      param.Biz_ID = appData.shopInfo.Biz_ID
      
      // if (co.isEmpty(param.sort)) {
      //    param.sort = 0
      // }
      if (td.edit_id == -1) {
         param.cateId = td.cate.idArr[td.pick_i]
         apiURL = 'table/Tmg/insertTableList'
      } else {
         var iArr = td.edit.split('-')
         var oneTableCate = td.tableList[iArr[0]].TableList[iArr[1]]
         // console.log("【获取桌子ID】===", oneTableCate)
         param.id = oneTableCate.id
         param.status = oneTableCate.status
         param.cateId = oneTableCate.cateId
         param.sort = oneTableCate.sort
         apiURL = 'table/Tmg/updateTableList'
      }
      // console.log("【获取桌子ID】===",param)
      app.getJsonData(apiURL, param, function (res) {
         // console.log('【返回更新插入信息】===', res.msg)
         t.setData({
            edit_id: -1,
            isShowInputLabelArr: [0, 0],
            mode: '',
            buttonText: '新增餐桌'
         })
         t.onLoad()
      })
   },
   // 获取餐桌数据
   getAllTableList(){
      var param = {
         Biz_ID: appData.shopInfo.Biz_ID,
         Where_Use: 'admin'
      }
      app.getJsonData('table/Tmg/getAllTableList', param, function (res) {
         if (res.code == 0) {
            var t_list = res.data
            var cate = {
               idArr: [],
               nameArr: []
            }
            for (var i = 0; i < t_list.length; i++) {
               cate.idArr.push(t_list[i].cateId)
               cate.nameArr.push(t_list[i].cateName)
            }
            _this.setData({
               tableList: res.data,
               cate: cate
            })
         }
      })
   },
   showInputLabel: function (e) {
      if (e.detail.value.length > 1) return;
      var index = Number(e.currentTarget.id);
      var isShowArray = this.data.isShowInputLabelArr;
      isShowArray[index] = true;

      this.setData({
         isShowInputLabelArr: isShowArray
      })
   },
   //删除餐桌
   delConfirm: function (e) {
      if (app.globalData.loginMode == 'boss') {
         var t = this
         app.confirm('重要提示', '确认删除吗？','取消', '删除', function (res) {
            console.log('delConfirm: function', res)
            if (res) { t.delTable(e) }
         })
      } else {
         app.toast('需要掌柜登录才能操作')
      }
   },
   delTable: function (e) {
      var t = this
      var e_id = e.currentTarget.id
      var e_ids = e_id.split('-');

      i = e_ids[0]
      j = e_ids[1]
      var arr = t.data.tableList
      var param = {
         cateId: arr[i].TableList[j].cateId,
         zhuo: arr[i].TableList[j].zhuo
      }

      app.getJsonData('table/Tmg/deleteTableList', param, function (res) {
         console.log("【获取返回数据】===",res.msg)
         if (res.code == 0) {
            arr[i].TableList.splice(j, 1)
            t.setData({ tableList: arr })
            // app.noticeUpdateTableListAdmin(res.data)
         }
      })
   },

})